package com.carryGo.service.lt;

import com.carryGo.dao.lt.BaseDao;
import com.carryGo.entity.PageManager;
import com.carryGo.entity.User;

import java.io.File;
import java.util.List;

/**
 * ClassName:UserService
 * package:
 * Desription:
 *
 * @Date:2020/8/12 19:30
 * @Author: tank_李涛
 *//*
 *FileName:  UserService
 * Author:   28633
 * Date  :   2020/8/12 19:30
 * */
public class UserService extends BaseDao {


    public boolean addUser(User user) {
        String sql = "INSERT INTO `rz1217db_4`.`user`(`username`, `password`, `salt`,`phone`) " +
                "VALUES (?, ?, ?, ?)";
        Object[]params = getObjArr(user);
        for (int i = 0; i < params.length; i++) {
        }
        int update = update(sql, params);
        System.out.println(update);
        return update > 0;
    }


    private Object[] getObjArr(User user) {
        Object[]params =
                {
                        user.getUsername(),user.getPassword(),user.getSalt(),user.getPhone()
                };
        return params;
    }

    //查询所有数据
    public List<User>queryUsers(PageManager pm){
        int start = (pm.getPn() -1) * pm.getRn();
        int end = pm.getRn();
        String sql = "select * from user limit ?,?";
        return findList(User.class,sql,start,end);
    }

    public boolean deleUser(int id) {
        String sql = "delete from user where id = ?";
        User user = queryUser(id);
        boolean result = update(sql, id) > 0;
        //如果结果为true则把本地的图片也要删除
       /* if (result){
            new File(user.getPic()).delete();
        }*/
        return result;
    }

    public User queryUser(int id) {
        String sql = "select * from user where id = ?";
        return findBean(User.class,sql,id);
    }

    public boolean updateUser(User user) {
        String sql = "UPDATE `rz1217db_4`.`user` SET `username` = ?, `password` = ?,`salt` = ?, `phone` = ? WHERE `id` = ?";
        Object[] params = {user.getUsername(), user.getPassword(), user.getSalt(), user.getPhone(),user.getId()};

       //查询单条数据
        User queryUser = queryUser(user.getId());

        int update = update(sql, params);
        System.out.println(update);
       /* try {
            boolean result = update(user) > 0;
            //更换头像成功之后将原头像删除
            if (result && queryUser.getPic() != null && !queryUser.getPic().equals(user.getPic())) {
                //删除原图片
                new File(queryUser.getPic()).delete();
            }
            return result;
        }catch (Exception e){
            e.printStackTrace();
        }*/
        return update > 0?true:false;
    }

    public int queryUserTotalCount(){
        String sql = "select count(*) from user";
        return count(sql);
    }

    public User queryUserByUsername(String uname){
        String sql = "select * from user where username = ?";
        return findBean(User.class,sql,uname);
    }


    /**
     * 查询所有数据
     */
    public List<User> queryUsers() {
        String sql = "select * from user";
        return findList(User.class,sql);
    }
}

